package router

import (
	"gitee.com/wangshh_123/cross-pay-go/src/config"
	mixingController "gitee.com/wangshh_123/cross-pay-go/src/controller/mixing"
	systemController "gitee.com/wangshh_123/cross-pay-go/src/controller/system"
	"gitee.com/wangshh_123/cross-pay-go/src/database"
	"gitee.com/wangshh_123/cross-pay-go/src/middleware"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/photonpay"
	"github.com/gin-gonic/gin"
)

// SetupStatisticsRouter 设置统计、卡费、汇率损失规则相关路由
func SetupStatisticsRouter(router *gin.RouterGroup, client *photonpay.PhotonPayClient) {
	statisticsController := systemController.NewStatisticsController(client)
	cardFeeController := mixingController.NewCardFeeController()
	exchangeLossRuleController := mixingController.NewExchangeLossRuleController(database.DB, config.GetPlatformConfig())

	// 统计路由组
	statistics := router.Group("/statistics")
	statistics.Use(middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware())
	{
		// 卡片统计
		statistics.GET("/cards", statisticsController.GetCardStatistics)
		// HlPay增强统计
		statistics.GET("/hlpay/enhanced", statisticsController.GetHlPayEnhancedStatistics)
	}

	// 卡费相关路由
	cardFeeGroup := router.Group("/card-fees")
	{
		cardFeeGroup.GET("", cardFeeController.GetCardFee)
		cardFeeGroup.GET("/all", cardFeeController.GetCardFees)
		cardFeeGroup.GET(":id", cardFeeController.GetCardFeeByID)
		cardFeeGroup.POST("", cardFeeController.CreateCardFee)
		cardFeeGroup.PUT(":id", cardFeeController.UpdateCardFee)
		cardFeeGroup.DELETE(":id", cardFeeController.DeleteCardFee)
	}

	// 汇率损失规则相关路由
	exchangeLossRuleGroup := router.Group("/exchange-loss-rule")
	exchangeLossRuleGroup.Use(middleware.AuthMiddleware())
	{
		// 获取当前用户的汇损规则
		exchangeLossRuleGroup.GET("/user", exchangeLossRuleController.GetUserExchangeLossRule)
	}

	// 汇率损失规则管理路由组（需要管理员权限）
	exchangeLossRuleAdminGroup := router.Group("/exchange-loss-rule")
	exchangeLossRuleAdminGroup.Use(middleware.AuthMiddleware(), middleware.AdminMiddleware())
	{
		// 查询汇损规则（支持查询用户特定规则或默认规则）- 不需要管理员权限
		exchangeLossRuleAdminGroup.GET("", exchangeLossRuleController.GetExchangeLossRule)
		// 获取汇损规则列表
		exchangeLossRuleAdminGroup.GET("/list", exchangeLossRuleController.GetExchangeLossRuleList)
		// 更新/创建汇损规则（如果用户没有特定规则则创建，有则更新）
		exchangeLossRuleAdminGroup.POST("", exchangeLossRuleController.UpdateExchangeLossRule)
		exchangeLossRuleAdminGroup.PUT("", exchangeLossRuleController.UpdateExchangeLossRule)
		// 删除用户特定汇损规则（通过 user_id 参数）
		exchangeLossRuleAdminGroup.DELETE("", exchangeLossRuleController.DeleteExchangeLossRule)

	}
}
